package com.cloudcompare.diff.builder;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import com.cloudcompare.diff.bean.Diff;
import com.cloudcompare.diff.bean.Differences;
import com.cloudcompare.diff.bean.Item;

/**
 * 
 * Generates html output for a Differences instance
 * 
 * @author Sean C. Sullivan
 *
 * 
 */
public class HtmlBuilder extends AbstractBuilder {

    public void build(OutputStream out, Differences d) {
        PrintWriter pw = new PrintWriter(out);

        pw.println("<html>");
        pw.println("<META http-equiv=\"Content-Type\" content=\"text/html\">");
        pw.println("<head>");
        pw.println("<title>File differences</title>");
        pw.println("</head>");

        pw.println("<body text=\"#000000\" vlink=\"#000000\" alink=\"#000000\" link=\"#000000\">");
        
        pw.println(getStyleTag());
        pw.print("<p>First file: ");
        String filename1 = d.getFileName1();

        if (filename1 == null) {
            filename1 = "filename1.zip";
        }
        pw.print(filename1);
        pw.println("<br>");

        pw.print("Second file: ");

        String filename2 = d.getFileName2();

        if (filename2 == null) {
            filename2 = "filename2.zip";
        }
        pw.print(filename2);
        pw.println("</p>");

        writeAdded(pw, d.getItems());
       
        pw.println("<hr>");
        pw.println("<p>");
        pw.println("Generated at " + new java.util.Date());
        pw.println("</p>");
        pw.println("</body>");

        pw.println("</html>");

        pw.flush();

    }

    protected void writeAdded(PrintWriter pw, List<Item> items) {
    	writeDiffSet(pw, "Added", items);
    }
    /**
     * White table
     * @param pw
     * @param name
     * @param items
     */
    protected void writeDiffSet(PrintWriter pw, String name, List<Item> items) {
    	pw.println("<TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" WIDTH=\"100%\" BORDER=\"0\">");
    	pw.println("<tr>");
        pw.println("<td class=\"diffs\" colspan=\"2\">" + name  + " (" + items.size() + " entries)</td>");
        pw.println("</tr>");
        for (Iterator<Item> iterator = items.iterator(); iterator.hasNext();) {
        	Item item = (Item) iterator.next();
        	System.out.println("Item:"+item.getName());
            pw.println("<tr>");
            	Diff diff=getDiff(item);
	            pw.println("<td>");
	            if(diff==null){
	            	pw.println(item.getName());
	            }
	            if(diff!=null && diff.getLeft()!=null && !diff.getLeft().equals("no")){
					pw.println(item.getName());
		        }
				pw.println("</td>");
				if(diff!=null){
					pw.println("<td>");
					pw.println("!=");
					pw.println("</td>");
					pw.println("<div style='display:none'>");
					pw.println("####Left:"+diff.getLeft());
					pw.println("####Right:"+diff.getRight());
					pw.println("####Diff:"+diff.getDiff());
					pw.println("</div>");
				}else{
					pw.println("<td>");
					pw.println("==");
					pw.println("</td>");
				}
				pw.println("<td>");
				if(diff==null){
	            	pw.println(item.getName());
	            }
		        if(diff!=null && diff.getRight()!=null && !diff.getRight().equals("no")){
					pw.println(item.getName());
			    }
				pw.println("</td>");
			pw.println("</tr>");
        }
        pw.println("</table>");
    }

	private Diff getDiff(Item item) {
		Diff diff=null;
		if(item.getDirectory()!=null){
//				pw.println("\n####Directory:");
			diff=item.getDirectory();
		}
		if(item.getText()!=null){
//				pw.println("\n####Text:");
			diff=item.getText();
		}	
		if(item.getDissasembly()!=null){
//				pw.println("\n####Dissasembly:");
			diff=item.getDissasembly();
		}
		if(item.getPresent()!=null){
//				pw.println("\n####Present:");
			diff=item.getPresent();
		}
		return diff;
	}

    protected String getStyleTag()
    {
        StringBuffer sb = new StringBuffer();
        
        sb.append("<style type=\"text/css\">");
        sb.append(" body, p { ");
        sb.append(" font-family: verdana,arial,helvetica; ");
        sb.append(" font-size: 80%; ");
        sb.append(" color:#000000; ");
        sb.append(" } \n");
        sb.append(" 	  .diffs { \n");
        sb.append("         font-family: verdana,arial,helvetica; \n");
        sb.append("         font-size: 80%; \n");
        sb.append(" font-weight: bold; \n");
        sb.append(" text-align:left; \n");
        sb.append(" background:#a6caf0; \n");
	  	sb.append(" } \n"); 
        sb.append(" tr, td { \n");
        sb.append(" font-family: verdana,arial,helvetica; \n");
        sb.append(" font-size: 80%; \n");
        sb.append(" background:#eeeee0; \n");
        sb.append(" } \n");  
        sb.append(" </style>\n");
        
        return sb.toString();
    }
}
